import unittest,time,logging
from selenium import webdriver
from selenium.webdriver.common.by import By
from Yuekao.Page.Page import Page

class TestCase(unittest.TestCase):

    def setUp(self):
        logging.info('start')
        # 选择浏览器
        self.driver = webdriver.Chrome()
        # 窗口最大化
        self.driver.maximize_window()

    def tearDown(self):
        time.sleep(5)
        # 退出浏览器
        self.driver.quit()
        logging.info('end')

    # 用例1登录
    def test_01_login(self):
        # 实例化Page
        po = Page(self.driver)
        # 打开a3mall网址
        po.open_url('http://10.59.9.12:7001/')
        # 异常处理
        try:
            # 点击登录文本
            po.click_text()
            # 输入账号
            po.send_user()
            # 输入密码
            po.send_pwd()
            # 强制等待输入验证码
            time.sleep(8)
            # 点击登录按钮
            po.click_button()
            # 强制等待直到断言元素出现
            time.sleep(5)
            # 断言
            dy = self.driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a').text
            self.assertEqual('欢迎回来，15523615757',dy)
        except Exception as e:
            # 异常时截图
            self.driver.save_screenshot('error.png')
            print(e)
    # 用例2搜索下单
    def test_02_search(self):
        # 调用登录
        self.test_01_login()
        # 实例化Page
        po = Page(self.driver)
        # 异常处理
        try:
            # 搜索框输入
            po.send_search()
            # 点击搜索按钮
            po.click_search()
            # 点击商品图片
            po.click_img()
            # 点击立即购买
            po.click_go()
            # 点击下一步
            po.click_next()
            # 强制等待断言元素
            time.sleep(3)
            # 断言
            dy = self.driver.find_element(By.XPATH,'/html/body/div[4]/div[1]/a[2]').text
            self.assertEqual('订单完成',dy)
        except Exception as e:
            # 异常时截图
            self.driver.save_screenshot('error.png')
            print(e)
    # 用例3团购下单
    def test_03_some(self):
        # 调用登录
        self.test_01_login()
        # 实例化Page
        po = Page(self.driver)
        # 异常处理
        try:
            # 点击团购入口
            po.click_some()
            # 点击商品图片
            po.click_some_img()
            # 点击立即购买
            po.click_go()
            # 点击下一步
            po.click_next()
            # 隐式等待断言元素
            self.driver.implicitly_wait(3)
            # 断言
            dy = self.driver.find_element(By.XPATH,'/html/body/div[4]/div[1]/a[2]').text
            self.assertEqual('确认订单',dy)
        except Exception as e:
            # 异常时截图
            self.driver.save_screenshot('error.png')
            print(e)